package com.tang.user.config;


import com.tang.common.exception.RecruitException;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;
import java.util.List;

@RestControllerAdvice("com.tang")
@Slf4j
public class GlobalExceptionHandler {
   /* @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        log.error("系统异常"+e.getMessage());
        e.printStackTrace();
        return Result.error(RecruitExceptionEnum.SYSTEM_ERROR);
    }

    @ExceptionHandler(RecruitException.class)
    public Result mallException(RecruitException e){
        log.error("业务异常"+e.getMessage());
        e.printStackTrace();
        return Result.error(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public  Result methodArgumentNotValid(MethodArgumentNotValidException e){
        List<String> list = new ArrayList<String>();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        for (ObjectError allError : allErrors) {
            list.add(allError.getDefaultMessage());
        }
        if(list.size() == 0){
            return Result.error(RecruitExceptionEnum.PARAMETER_ERROR);
        }else{
            return Result.error(RecruitExceptionEnum.PARAMETER_ERROR.getCode(),list.toString());
        }
    }*/
}
